/**
 ******************************************************
 * 作者:朱雍
 * 备注:
 * 时间:2017年3月17日上午7:14:45
 * Copyright (c) 2017, 243813208@qq.com All Rights Reserved.
 ******************************************************
 */

package cn.sxt.yxl.common.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
 * 类名:HttpUtil 
 * 备注:	 
 * 时间:2017年3月17日 上午7:14:45
 * 作者:朱雍
 * @since  	JDK 1.8	 
 */
public class HttpUtil {
	/**
	 * 模拟get请求
	 * 方法名:methodGet
	 * 备注:
	 * 作者:朱雍
	 * 时间:2017年3月17日上午7:16:35
	 * @param urlStr
	 * @param headerMap
	 * @param paramsMap
	 * @return
	 * @since JDK 1.8
	 */
	public String methodGet(String urlStr,Map<String, String> headerMap,Map<String, String> paramsMap){
		StringBuffer sb = new StringBuffer();
		//处理URL传参
		if(paramsMap.size() > 0){
			//传参
			if(urlStr.indexOf("?") == -1){
				urlStr += "?";
			}else{
				urlStr += "&";
			}
			//遍历参数加在URL后面
			Set<Entry<String, String>> entrySet = paramsMap.entrySet();
			for (Entry<String, String> entry : entrySet) {
				String key = entry.getKey() +"";
				String value = entry.getValue() +"";
				urlStr += key+"="+value+"&";
			}
		}
		try {
			URL url = new URL(urlStr);
			//打开一个URL链接
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			//设置请求头
			Set<Entry<String, String>> headerSet = headerMap.entrySet();
			for (Entry<String, String> entry : headerSet) {
				String key = entry.getKey();
				String value = entry.getValue();
				connection.setRequestProperty(key, value);
			}
			//状态码
			int code = connection.getResponseCode();
			ConstatFinalUtil.LOGGER.info("状态码：{}",code);
			InputStream is = null;
			if(code == 200){
				is = connection.getInputStream();
			}else{
				is = connection.getErrorStream();
			}
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			String temp = "";
			while((temp = br.readLine()) != null){
				sb.append(temp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sb.toString();
	}
	
	/**
	 * 模拟post请求
	 * 方法名:methodGet
	 * 备注:
	 * 作者:朱雍
	 * 时间:2017年3月17日上午7:16:35
	 * @param urlStr
	 * @param headerMap
	 * @param paramsMap
	 * @return
	 * @since JDK 1.8
	 */
	public String methodPost(String urlStr,Map<String, String> headerMap,Map<String, String> paramsMap){
		StringBuffer sb = new StringBuffer();
		
		try {
			URL url = new URL(urlStr);
			//打开一个URL链接
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setDoInput(true);
			connection.setDoOutput(true);
			//设置请求头
			Set<Entry<String, String>> headerSet = headerMap.entrySet();
			for (Entry<String, String> entry : headerSet) {
				String key = entry.getKey();
				String value = entry.getValue();
				connection.setRequestProperty(key, value);
			}
			//设置提交的方式
//			connection.setRequestMethod("POST");
			
			//设置参数
			StringBuffer paramsSb = new StringBuffer();
			Set<Entry<String, String>> entrySet = paramsMap.entrySet();
			for (Entry<String, String> entry : entrySet) {
				String key = entry.getKey() +"";
				String value = entry.getValue() +"";
				paramsSb.append(key+"="+value+"&");
			}
			
			//输出
			OutputStream os = connection.getOutputStream();
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
			bw.write(paramsSb.toString());
			bw.flush();
			bw.close();
			//状态码,一定要在请求完成后获取，也就是输出流完成后，否则post方式会变成get方式，也可以设置setRequestMethod("POST")解决
			int code = connection.getResponseCode();
			ConstatFinalUtil.LOGGER.info("状态码：{}",code);
			InputStream is = null;
			if(code == 200){
				is = connection.getInputStream();
			}else{
				is = connection.getErrorStream();
			}
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			String temp = "";
			while((temp = br.readLine()) != null){
				sb.append(temp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sb.toString();
	}
}

